package com.xcl.bili.api;


import com.xcl.bili.domain.User;
import com.xcl.bili.domain.exception.JsonResponse;
import com.xcl.bili.service.UserService;
import com.xcl.bili.service.utils.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserApi {

    @Autowired
    private UserService userService;

    @GetMapping("/rsa-pks")
    public JsonResponse<String> getRsaPublicKey(){
        String pk = RSAUtil.getPublicKeyStr();
        return new JsonResponse<>(pk);
    }

    /*
    1. 注册用户
     */
    @PostMapping("/user")
    public JsonResponse<String> addUser(@RequestBody User user){
       userService.addUser(user);
       return JsonResponse.success();
    }

    /*
    2. 登录返回jwt token
     */
    @PostMapping("/user-token")
    public JsonResponse<String> login(@RequestBody User user) throws Exception {
        String token = userService.login(user);
        return JsonResponse.success(token);
    }
}
